<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title></title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="/js/layui/css/layui.css" media="all">
    <style>
        .layui-input, .layui-textarea {
            width: 70%;
            display: inline-block;
        }
    </style>
</head>
<body style="width:100%;margin:15px auto;overflow:auto;">
<form class="layui-form" action="">
    <div class="layui-form-item">
        <div class="layui-input-block">
            <span style="">选择导出方式</span>
            <a class="layui-btn layui-btn-xs" id="exportExcel">导出带员工照片Excel</a>
            <a class="layui-btn layui-btn-xs" id="exportExcel2">导出不带员工照片Excel</a>
        </div>
    </div>

    <div class="layui-form-item">
        <div class="layui-input-block">
            <span>注：导出带员工照片Excel可能会时间较长，请耐心等待</span>
        </div>
    </div>
</form>

<script src="/js/layui/layui.js"></script>
<script src="/js/layui/layui.all.js"></script>
<script src="/admin/js/token.js"></script>
<script src="./js/jquery.min.js"></script>
<script src="./js/jquery.fileDownload.js"></script>
<script>
    //url 传参
    function GetQueryString(name){
        var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
        var a = decodeURI(window.location.search);
        var r = a.substr(1).match(reg);//search,查询？后面的参数，并匹配正则
        if(r!=null)return  unescape(r[2]); return null;
    }

    function getStatus(times){
        var result = 0
        $.ajax({
            type:"post",
            async: false,
            url:"/staff/getExportStaffStatus",
            data:{"times":times},
            success:function (res) {
                result = res.data
            }
        });
        return result;
    }

    $('#exportExcel').on('click',function () {
        var staffName = GetQueryString("staffName");
        var wuye = GetQueryString("wuye");
        var qiye = GetQueryString("qiye");
        var zhiye =GetQueryString("zhiye");
        var guoji =GetQueryString("guoji");
        var status = GetQueryString("status");
        var exportType = 1;
        var httpMethod = 'get';
        var url = "/staff/exportExcelByStaff";
        var params = {
            staffName: staffName, wuye: wuye, qiye: qiye, zhiye: zhiye, guoji: guoji, status: status, exportType: exportType
        };
        $.ajax({
            type:"post",
            url:url,
            data:params,
            success:function (res) {
                var index = layui.layer.load();
                var time = res.data;
                var t = setInterval(function () {
                    var result = getStatus(time);
                    if (result.status == "1") {
                        layer.close(index);
                        clearInterval(t);
                        var a = document.createElement("a")
                        a.href = result.downloadUrl ;
                        a.click()
                    }else if(result.status == "2"){
                        layer.close(index);
                        clearInterval(t);
                        layer.msg("导出失败",{icon:2});
                    }
                }, 1000);
            }
        });
        // $.fileDownload(url, {
        //     httpMethod: httpMethod,
        //     // 因为这个插件貌似只支持URL参数,所以要用jquery param将js对象转成URL参数
        //     data: params,
        //     prepareCallback: function (url) {
        //         layui.layer.load();
        //     },
        //     //要想成功回调失败和成功方法，要在响应首部加 response.setHeader("Set-Cookie", "fileDownload=true; path=/");
        //     successCallback: function (url) {
        //         // layer.msg("导出成功！");
        //         // var index = layui.layer.load();
        //         // layer.close(index);
        //         console.log("url=="+url);
        //         var index = layui.layer.load();
        //         var time = url.data;
        //         var t = setTimeout(function () {
        //             var result = getStatus(time);
        //             console.log("result=="+result);
        //             if (result == "1"){
        //                 layer.close(index);
        //                 clearTimeout(t);
        //             }
        //         },5000)
        //     },
        //     failCallback: function (html, url) {
        //         layer.msg("导出失败！");
        //         var index = layui.layer.load();
        //         layer.close(index);
        //     }
        // })

    });

    $('#exportExcel2').on('click',function () {
        var staffName = GetQueryString("staffName");
        var wuye = GetQueryString("wuye");
        var qiye = GetQueryString("qiye");
        var zhiye =GetQueryString("zhiye");
        var guoji =GetQueryString("guoji");
        var status = GetQueryString("status");
        var exportType = 2;
        var httpMethod = 'get';
        var url = "/staff/exportExcelByStaff";
        var params = {
            staffName: staffName, wuye: wuye, qiye: qiye, zhiye: zhiye, guoji: guoji, status: status, exportType: exportType
        };
        $.fileDownload(url, {
            httpMethod: httpMethod,
            // 因为这个插件貌似只支持URL参数,所以要用jquery param将js对象转成URL参数
            data: params,
            prepareCallback: function (url) {
                layui.layer.load();
            },
            //要想成功回调失败和成功方法，要在响应首部加 response.setHeader("Set-Cookie", "fileDownload=true; path=/");
            successCallback: function (url) {
                layer.msg("导出成功！");
                var index = layui.layer.load();
                layer.close(index);
            },
            failCallback: function (html, url) {
                layer.msg("导出失败！");
                var index = layui.layer.load();
                layer.close(index);
            }
        })
    });
</script>
</body>
</html>